﻿//根据一个点 设置角度和半径得出另外一个点
function TriangleLengthXY(coordinates: MyCoordinates, rf: number, length: number) {
    let coor = new MyCoordinates(0, 0);
    if (rf == 90 || rf == 360) {
        coor.x = coordinates.x;
        coor.y = coordinates.y + length;
    } else if (rf == 0) {
        coor.x = coordinates.x + length;
        coor.y = coordinates.y;
    } else if (rf == 180) {
        coor.x = coordinates.x - length;
        coor.y = coordinates.y;
    } else if (rf == 270) {
        coor.x = coordinates.x;
        coor.y = coordinates.y - length;
    } else {
        let s = parseInt(rf / 90 + "");
        let rfs = rf - s * 90;
        let x: number;
        let y: number;
        let a = rfs / 180 * Math.PI;
        x = Math.cos(a) * length;
        y = Math.sin(a) * length;

        switch (s) {
            case 0:
                coor.x = x;
                coor.y = y;
                break;
            case 1:
                coor.x = -y;
                coor.y = x;
                break;
            case 2:
                coor.x = -x;
                coor.y = -y;
                break;
            case 3:
                coor.x = y;
                coor.y = -x;
                break;
        }

        coor.x += coordinates.x;
        coor.y += coordinates.y;
    }

    return coor;
}
//以开始点为圆点  计算另一个点的角度
function TriangleRadii(startDoc: MyCoordinates, endDoc: MyCoordinates) {
    let doc = endDoc.subtraction(startDoc);
    if (doc.x > 0 && doc.y > 0) {
        return Math.atan(doc.y / doc.x) / Math.PI * 180;
    } else if (doc.x < 0 && doc.y > 0) {
        return Math.atan((-doc.x) / doc.y) / Math.PI * 180 + 90;
    } else if (doc.x < 0 && doc.y < 0) {
        return Math.atan((-doc.y) / (-doc.x)) / Math.PI * 180 + 180;
    } else if (doc.x > 0 && doc.y < 0) {
        return Math.atan(doc.x / (-doc.y)) / Math.PI * 180 + 270;
    } else if (doc.x >= 0 && doc.y == 0) {
        return 0;
    } else if (doc.x == 0 && doc.y > 0) {
        return 90;
    } else if (doc.x < 0 && doc.y == 0) {
        return 180;
    } else if (doc.x == 0 && doc.y < 0) {
        return 270;
    }
    return 0;

}
//计算两点距离
function PointDistance(startDoc: MyCoordinates, endDoc: MyCoordinates) {
    let doc = endDoc.subtraction(startDoc);
    return Math.sqrt(Math.abs(doc.x * doc.x) + Math.abs(doc.y * doc.y));
}

class MyCoordinates {
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    subtraction(var1: MyCoordinates) {
        return new MyCoordinates(this.x - var1.x, this.y - var1.y);
    }

    isCoordinates(var1: MyCoordinates) {
        return this.x == var1.x && this.y == var1.y;
    }

}

